package org.ktjy.quartz_demo.controller;

import org.ktjy.quartz_demo.entity.JobBean;
import org.ktjy.quartz_demo.job.Quartz_BackUpDB;
import org.ktjy.quartz_demo.job.Quartz_SendEmail;
import org.ktjy.quartz_demo.util.JobUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jane
 * @date 2024-12-16 15:58
 */
@RestController
@RequestMapping("/api")
public class QuartzControllerSendEmail {

    @Autowired
    Scheduler scheduler;

    private String jobName="sendEmail";
    @RequestMapping("/create")
    public String createQuartz(){
//        JobBean jobBean= new JobBean("sendEmail","org.ktjy.quartz_demo.job.SendEmailJob","0/5 * * * * ?");
        JobBean jobBean= new JobBean(jobName, Quartz_SendEmail.class.getName(),"0/5 * * * * ?");
        try {
            JobUtils.createJob(scheduler,jobBean);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return "ok";
    }
    @RequestMapping("/pausue")
    public String pausueQuartz(){
        JobUtils.pausueJob(scheduler,jobName);
        return "pausue-ok";
    }
    @RequestMapping("/resume")
    public String resumeQuartz(){
        JobUtils.resumeJob(scheduler,jobName);
        return "resume-ok";
    }
    @RequestMapping("/delete")
    public String deleteQuartz(){
        JobUtils.deleteJob(scheduler,jobName);
        return "delete-ok";
    }
    @RequestMapping("/once")
    public String onceJob(){
        JobUtils.onceJob(scheduler,jobName);
        return "once-ok";
    }
    @RequestMapping("/update")
    public String updateJob(){
        JobBean bean = new JobBean(jobName,Quartz_SendEmail.class.getName(),"0/2 * * * * ?");
        JobUtils.updateJob(scheduler,bean);
        return "update-ok";
    }
}
